package com.zl.cms.service;

import com.google.common.base.Optional;
import com.zl.basic.CommonUtil.Check;
import com.zl.basic.model.Pager;
import com.zl.basic.model.SystemContext;
import com.zl.cms.dao.IAttachmentDao;
import com.zl.cms.dao.IChannelDao;
import com.zl.cms.dao.ITopicDao;
import com.zl.cms.dao.IUserDao;
import com.zl.cms.model.Attachment;
import com.zl.cms.model.Channel;
import com.zl.cms.model.Topic;
import com.zl.cms.model.User;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * User: zl email:c3tc3tc3t@126.com Date: 14-3-16 Time: 下午4:30
 */
@Service("topicService")
public class TopicService implements ITopicService {
  private ITopicDao topicDao;
  private IAttachmentDao attachmentDao;
  private IChannelDao channelDao;
  private IUserDao userDao;


  public IUserDao getUserDao() {
    return userDao;
  }

  @Inject
  public void setUserDao(IUserDao userDao) {
    this.userDao = userDao;
  }

  public IChannelDao getChannelDao() {
    return channelDao;
  }

  @Inject
  public void setChannelDao(IChannelDao channelDao) {
    this.channelDao = channelDao;
  }

  public ITopicDao getTopicDao() {
    return topicDao;
  }

  @Inject
  public void setTopicDao(ITopicDao topicDao) {
    this.topicDao = topicDao;
  }

  public IAttachmentDao getAttachmentDao() {
    return attachmentDao;
  }

  @Inject
  public void setAttachmentDao(IAttachmentDao attachmentDao) {
    this.attachmentDao = attachmentDao;
  }

  @Override
  public void add(Topic topic, int cid, int uid, Integer[] aids) {

    Check.checkAnyNull(cid, " cid is null");
    Check.checkAnyNull(uid, "uid is null");
    Check.checkAnyNull(topic, "topic is null");

    Channel channel = channelDao.load(cid);
    User user = userDao.load(uid);
    Check.checkAnyNull(user, "添加文章的用户不存在");
    Check.checkAnyNull(channel, "添加到文章的栏目不存在");
    topic.setAuthor(user.getNickname());
    topic.setCname(channel.getName());
    topic.setCreateDate(new Date());
    topic.setChannel(channel);
    topic.setUser(user);
    topicDao.add(topic);
    addTopicAtts(topic, aids);
  }

  @Override
  public void add(Topic topic, int cid, int uid) {
    this.add(topic, cid, uid, null);
  }

  @Override
  public void updateStatus(int tid) {
    Topic t = topicDao.load(tid);
    Check.checkAnyNull(t, "文章不存在");
    if (t.getStatus() == 0) {
      t.setStatus(1);
    } else {
      t.setStatus(0);
    }
    topicDao.update(t);
  }

  @Override
  public void delete(int id) {
    List<Attachment> atts = attachmentDao.listAttachByTopic(id);
    attachmentDao.deleteByTopic(id);
    topicDao.delete(id);
    // 删除硬盘上的文件

    for (Attachment a : atts) {
      AttachmentService.deleteAttachmentFiles(a);
    }
  }

  private void addTopicAtts(Topic topic, Integer[] aids) {
    for (Integer aid : aids) {
      Attachment a = attachmentDao.load(aid);
      if (null == a) {
        continue;
      } else {
        a.setTopic(topic);
      }
    }
  }

  @Override
  public void update(Topic topic, int cid, Integer[] aids) {
    Channel channel = channelDao.load(cid);
    Check.checkAnyNull(channel, "更新文章所属的栏目不存在");
    Check.checkAnyNull(topic, "被更新的文章没有传递过来");
    topic.setCname(channel.getName());
    topic.setChannel(channel);
    addTopicAtts(topic, aids);
    topicDao.update(topic);
  }

  @Override
  public void update(Topic topic, int cid) {
    update(topic, cid, null);
  }

  @Override
  public Topic load(int id) {
    return topicDao.load(id);
  }

  @Override
  public Pager<Topic> find(Integer cid, String title, Integer status) {
    return topicDao.find(cid, title, status);
  }

  @Override
  public Pager<Topic> find(Integer uid, Integer cid, String title, Integer status) {
    return topicDao.find(uid, cid, title, status);
  }

  @Override
  public Pager<Topic> searchTopicByKeyword(String keyword) {
    return topicDao.searchTopicByKeyword(keyword);
  }

  @Override
  public Pager<Topic> searchTopic(String con) {
    return topicDao.searchTopic(con);
  }

  @Override
  public Pager<Topic> findRecommendTopic(Integer cid) {
    return topicDao.findRecommendTopic(cid);
  }
}
